package com.bonc.event.eventtype;

import java.util.Map;
import java.util.Set;

import com.bonc.utilities.EventUtility;
import com.bonc.vectorspace.model.EventDocument;

import cc.mallet.classify.Classifier;
import cc.mallet.types.Alphabet;
import cc.mallet.types.FeatureVector;
import cc.mallet.types.Instance;
import cc.mallet.types.LabelAlphabet;

/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年7月4日 上午10:36:09
 */
public class EventTypeClassifier {

	public static String predictEventType(EventTypeTrainer eventTypeTrainer, String text, Map<String, String> stopwords){
		
		Classifier classifier = eventTypeTrainer.getClassifier();
		
		if(text !=null && !"".equals(text.trim())){

			//classify,predict
			Alphabet featureAlphabet = new Alphabet();//特征词典
			LabelAlphabet targetAlphabet = new LabelAlphabet();//类标词典

			Set<String> labels = eventTypeTrainer.getTrainingCorpus().getTags();
			for(String label:labels){
				targetAlphabet.lookupIndex(label);
			}
			targetAlphabet.stopGrowth();


			Set<String> featureTerms = eventTypeTrainer.getFeatureTerms();
			int featuresize = featureTerms.size();
			EventDocument document = new EventDocument("test-1", text,"test-1", stopwords);
			double[] featureValues1 = new double[featureTerms.size()];

			int j = 0;
			for (String term : featureTerms) {				
				// 				Double weight = weights.get(term);      		
//				double frequency = document.getTermFrequency(term);	
				double tf = document.getTermFrequency(term);
				double idf = eventTypeTrainer.getTrainingCorpus().getInverseDocumentFrequency(term);
				double weight = tf * idf;
				featureValues1[j] = weight;				
				j++;				
			}		
			System.out.println("=======================================2");

			FeatureVector featureVector = new FeatureVector(featureAlphabet,featureTerms.toArray(new String[featuresize]),featureValues1);//change list to array           
			System.out.println("test1=="+document.getTag());
			Instance testInstance = new Instance (featureVector,targetAlphabet.lookupLabel(document.getTag()), document.getDocId(),null);

			String predictResult = EventTypeTrainer.predict(classifier,testInstance);
			return predictResult;
		}
		return null;
	}

}
